package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JComboBox;

import bus.QuestionBus;
import bus.QuizzesBus;
import bus.StudentResultBus;
import dao.StudentAnswerDao;
import dao.SubjectDao;
import dto.AnswersDto;
import dto.QuestionsDto;
import dto.QuizzesDto;
import dto.StudentAnswerDto;
import dto.SubjectDto;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;

import java.awt.GridLayout;

import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class JFrameStudentAttemptQuiz extends JFrame {

	JRadioButton rdbA, rdbB, rdbC, rdbD;
	private JPanel contentPane;
	private JComboBox cboSubject;
	private JComboBox cboQuizNo;
	private JTextArea txtQuestion;
	private ButtonGroup group = new ButtonGroup();
	private JLabel lbQuestionNo;
	private int noQuestion;
	private ArrayList<QuestionsDto> listQuestions = new ArrayList<>();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrameStudentAttemptQuiz frame = new JFrameStudentAttemptQuiz();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JFrameStudentAttemptQuiz() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {

				SubjectDao subjectDao = new SubjectDao();
				ArrayList<SubjectDto> allSubject = subjectDao.getAllSubject();
				for (SubjectDto subjectDto : allSubject) {
					cboSubject.addItem(subjectDto.getSubjectName());
				}
			}

			@Override
			public void windowClosing(WindowEvent e) {
				JFrameStudentMainPage jFrameStudentMainPage = new JFrameStudentMainPage();
				jFrameStudentMainPage.setVisible(true);
			}
		});

		setResizable(false);
		setTitle("Student - Attemp Quiz");
		setBounds(100, 100, 651, 523);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("Subject:");
		lblNewLabel.setBounds(64, 23, 54, 14);
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		contentPane.add(lblNewLabel);

		JLabel lblQuizNo = new JLabel("Quiz no:");
		lblQuizNo.setBounds(355, 23, 61, 14);
		lblQuizNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
		contentPane.add(lblQuizNo);

		cboSubject = new JComboBox();
		cboSubject.setFont(new Font("Tahoma", Font.PLAIN, 15));
		cboSubject.setBounds(120, 14, 151, 30);
		cboSubject.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				clearGui();
				cboQuizNo.removeAllItems();
				String subjectName = (String) cboSubject.getSelectedItem();
				ArrayList<QuizzesDto> arrQuizzes = QuizzesBus
						.getQuizzesBySubjectName(subjectName);
				for (QuizzesDto quiz : arrQuizzes) {
					cboQuizNo.addItem(String.valueOf(quiz.getQuizNo()));
				}
			}
		});
		contentPane.add(cboSubject);

		cboQuizNo = new JComboBox();
		cboQuizNo.setFont(new Font("Tahoma", Font.PLAIN, 15));
		cboQuizNo.setBounds(418, 14, 173, 30);
		cboQuizNo.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				clearGui();
				String quizNoStr = (String) cboQuizNo.getSelectedItem();
				try {
					int quizNo = Integer.parseInt(quizNoStr);
					listQuestions = QuestionBus.getQuestionByQuizNo(quizNo);
					if (listQuestions.size() > 0) {
						noQuestion = 0;

						loadQuestion();
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		});
		contentPane.add(cboQuizNo);

		txtQuestion = new JTextArea();
		txtQuestion.setFont(new Font("Monospaced", Font.PLAIN, 17));
		txtQuestion.setEditable(false);
		txtQuestion.setBounds(10, 88, 620, 109);
		contentPane.add(txtQuestion);

		JLabel lblQuestion = new JLabel("Question");
		lblQuestion.setBounds(10, 63, 61, 14);
		lblQuestion.setFont(new Font("Tahoma", Font.PLAIN, 15));
		contentPane.add(lblQuestion);

		lbQuestionNo = new JLabel("1");
		lbQuestionNo.setBounds(76, 63, 61, 14);
		lbQuestionNo.setFont(new Font("Tahoma", Font.PLAIN, 15));
		contentPane.add(lbQuestionNo);

		JButton btnBack = new JButton("<< Back");
		btnBack.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (checkDataInput()) {
					if (listQuestions.size() > 0) {
						QuestionsDto question = listQuestions.get(noQuestion);
						insertStudentAnswer(question);
					}
					clearGui();
					noQuestion--;
					if (noQuestion < 0)
						noQuestion = 0;
					loadQuestion();
				}
			}
		});
		btnBack.setBounds(157, 411, 89, 23);
		contentPane.add(btnBack);

		JButton btnNext = new JButton("Next >>");
		btnNext.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (checkDataInput()) {
					if (listQuestions.size() > 0) {
						QuestionsDto question = listQuestions.get(noQuestion);
						insertStudentAnswer(question);
					}
					clearGui();
					noQuestion++;
					if (noQuestion >= listQuestions.size())
						noQuestion = listQuestions.size() - 1;
					loadQuestion();
				}
			}
		});
		btnNext.setBounds(274, 411, 89, 23);
		contentPane.add(btnNext);

		JPanel panel = new JPanel();
		panel.setBounds(10, 208, 620, 174);
		contentPane.add(panel);
		panel.setLayout(new GridLayout(4, 1, 0, 0));

		rdbA = new JRadioButton("A");
		rdbA.setFont(new Font("Tahoma", Font.PLAIN, 15));
		panel.add(rdbA);
		rdbA.setAutoscrolls(true);

		rdbB = new JRadioButton("B");
		rdbB.setFont(new Font("Tahoma", Font.PLAIN, 15));
		panel.add(rdbB);

		rdbC = new JRadioButton("C");
		rdbC.setFont(new Font("Tahoma", Font.PLAIN, 15));
		panel.add(rdbC);

		rdbD = new JRadioButton("D");
		rdbD.setFont(new Font("Tahoma", Font.PLAIN, 15));
		panel.add(rdbD);

		JButton btnFinish = new JButton("Finish");
		btnFinish.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (listQuestions.size() > noQuestion
						&& listQuestions.size() > 0) {
					QuestionsDto question = listQuestions.get(noQuestion);
					insertStudentAnswer(question);
				}

				try {
					int quizNo = Integer.parseInt((String) cboQuizNo
							.getSelectedItem());
					StudentResultBus.markResultOfStudent(
							JFrameStudentMainPage.ID, quizNo);

				} catch (Exception e2) {
					// TODO: handle exception
				}

				JFrameStudentMainPage jFrameStudentMainPage = new JFrameStudentMainPage();
				jFrameStudentMainPage.setVisible(true);
				dispose();
			}
		});
		btnFinish.setBounds(387, 411, 89, 23);
		contentPane.add(btnFinish);

		group.add(rdbA);
		group.add(rdbB);
		group.add(rdbC);
		group.add(rdbD);
	}

	private boolean checkDataInput() {
		if (!rdbA.isSelected() && !rdbB.isSelected() && !rdbC.isSelected()
				&& !rdbD.isSelected()) {
			JOptionPane.showMessageDialog(this, "You have not choesed answer.");
			return false;
		}

		return true;
	}

	private void loadQuestion() {
		if (listQuestions.size() > noQuestion && listQuestions.size() > 0) {
			QuestionsDto question = listQuestions.get(noQuestion);
			lbQuestionNo.setText(String.valueOf(noQuestion + 1));
			txtQuestion.setText(question.getQuestionDesc());
			txtQuestion.setToolTipText(listQuestions.get(noQuestion).getHint());

			AnswersDto answer = QuestionBus.getAnsersByQuestionNo(question
					.getQuestionNo());
			rdbA.setText(answer.getCaseA());
			rdbB.setText(answer.getCaseB());
			rdbC.setText(answer.getCaseC());
			rdbD.setText(answer.getCaseD());

			// insertStudentAnswer(question);
		}
	}

	private void insertStudentAnswer(QuestionsDto question) {
		int answerNo = 0;
		if (rdbA.isSelected())
			answerNo = 1;
		if (rdbB.isSelected())
			answerNo = 2;
		if (rdbC.isSelected())
			answerNo = 3;
		if (rdbD.isSelected())
			answerNo = 4;
		if (answerNo > 0) {
			StudentAnswerDto studentAnswer = new StudentAnswerDto(
					JFrameStudentMainPage.ID, question.getQuestionNo(),
					answerNo);
			StudentAnswerDao studentAnswerDao = new StudentAnswerDao();
			studentAnswerDao.insertStudentAnswer(studentAnswer);

		}
	}

	private void clearGui() {
		txtQuestion.setText("");
		lbQuestionNo.setText("");
	}
}
